﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iCollect;
using iCollect.Events;
using System.Data.SqlClient;
using iCollect.CUPDataDataSet3TableAdapters;
using iCollect.Constants;

/*
 * 
 * 中国石油
 * 中国石化
 * 海洋石油
 * 中航油
 * 
 */
namespace iCollect.login
{
    public partial class LoginForm : Form
    {
        public const string ADMIN = "admin";
        public const string NORMAL = "normal";
        public const string EXIT = "exit";

        public event EventHandler<CustomEventArgs> RaiseCustomEvent;

        public LoginForm()
        {
            InitializeComponent();
        }

        protected virtual void OnRaiseCustomEvent(CustomEventArgs e)
        {
            EventHandler<CustomEventArgs> handler = RaiseCustomEvent;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        public void button1_Click(object sender, EventArgs e)
        {
            login();
        }

        public void login()
        {
            string inputUser = textBox2.Text;
            string inputPassword = textBox3.Text;
            DataRow[] foundRows;

            if (inputUser == null || inputUser == "" ||
                inputPassword == null || inputPassword == "")
            {
                inputError("请输入用户名和密码");
                return;
            }

            try
            {
                userTableAdapter adapter = new userTableAdapter();

                DataSet users = new DataSet();
                adapter.Fill(cUPDataDataSet3.user);

                // Presuming the DataTable has a column named Date.
                string expression;
                expression = "user = '" + inputUser + "' AND " + "password = '" + inputPassword + "'";
                
                // Use the Select method to find all rows matching the filter.
                foundRows = cUPDataDataSet3.user.Select(expression);
                if (foundRows.Length == 0)
                {
                    inputError("用户名或密码输入错误");
                    return;
                }

            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
            }
 

            if (textBox2.Text == "admin")
            {
                OnRaiseCustomEvent(new CustomEventArgs(ADMIN, 0));
                GlobalConstants.isAdmin = true;
            }
            else
            {
                OnRaiseCustomEvent(new CustomEventArgs(NORMAL, 0));
            }
            this.Close();
        }

        private void textBox3_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            // Determine whether the keystroke is a number from the top of the keyboard.
            if (e.KeyCode == Keys.Enter)
            {
                login();
            }
        }



        private void inputError(string msg)
        {
            DialogResult MsgBoxResult;//设置对话框的返回值
            MsgBoxResult = MessageBox.Show(msg,//对话框的显示内容
            "提示",//对话框的标题
            MessageBoxButtons.OK,//定义对话框的按钮，这里定义了YSE和NO两个按钮
            MessageBoxIcon.Exclamation,//定义对话框内的图表式样，这里是一个黄色三角型内加一个感叹号
            MessageBoxDefaultButton.Button1);//定义对话框的按钮式样
        }


        private void button2_Click(object sender, EventArgs e)
        {
            OnRaiseCustomEvent(new CustomEventArgs(EXIT, 0));
            this.Close();
        }

    }
}
